package com.example.module.ai.controller;

import com.example.module.ai.api.AiApi;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
@Slf4j
public class AiController {

    @Resource
    private AiApi deepSeekOllamaAiApiImpl;

    @Resource
    private AiApi tongYiAiApiImpl;

    @Resource
    private AiApi deepSeekAppTencentWebSocketAiApiImpl;

    @GetMapping("/tongyi/example")
    public String tongyiCompletion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return tongYiAiApiImpl.completion(message);
    }

    @GetMapping("/tencent/example")
    public String tencentCompletion(@RequestParam(required = false) String sessionId, @RequestParam(required = false) String reqId, @RequestParam(value = "message", defaultValue = "你是谁？") String message) {
        return deepSeekAppTencentWebSocketAiApiImpl.completion(message, sessionId, reqId);
    }

    @GetMapping("/ollama/example")
    public String ollamaCompletion(@RequestParam(value = "message", defaultValue = "你是谁？") String message) {
        return deepSeekOllamaAiApiImpl.completion(message);
    }

    /**
     * Spring WebFlux 提供了对 SSE 的原生支持
     *
     * @param message 提问消息
     * @return 回答流
     */
    @PermitAll // 解决 SSE 最终响应的时候，会被 Access Denied 拦截的问题
    @GetMapping(value = "/tongyi/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> tongyiStreamCompletion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return tongYiAiApiImpl.streamCompletion(message);
    }

}
